package ooday04;

import java.util.Scanner;

//多态测试类
public class Test {
    public static void main(String[] args) {

        Animal[] aniamal = new Animal[5];
        aniamal[0] = new Dog("小黑", 2, "白");
        aniamal[1] = new Dog("小白", 2, "黑");
        aniamal[2] = new Fish("小黄", 5, "黄");
        aniamal[3] = new Fish("小金", 2, "金");
        aniamal[4] = new Chick("小花", 1, "花");

            for (int i = 0; i < aniamal.length; i++) { //遍历所有动物
                System.out.println(aniamal[i].name); //输出每个动物的名字
                aniamal[i].eat(); //每个动物吃饭
                aniamal[i].drink(); //每个动物喝水

                if (aniamal[i] instanceof Dog) { //若为Dog
                    Dog dog = (Dog) aniamal[i];
                    dog.lookhome();

                }
                if (aniamal[i] instanceof Chick) { //若为Chick
                    Chick chick = (Chick) aniamal[i];
                    chick.layEggs();
                }

//                if (aniamal[i] instanceof Fish) { //若为Fish
//                    Fish fish = (Fish) aniamal[i];
//                    fish.swim();
//                }
                if (aniamal[i] instanceof Swim){//若为Chick
                    Swim s  = (Swim)aniamal[i];
                    s.swim();
                }

            }
/*

        //演示 向上造型(多态)的第二点应用:
        Master master = new Master();
        Dog dog = new Dog("小黑",2,"白");
        Fish fish = new Fish("小白",2,"黑");
        Chick chick = new Chick("小金",2,"银");

        master.feed(dog);//再传参的同时,系统自动做了向上造型
        master.feed(fish);
        master.feed(chick);
        master.drink(dog);//再传参的同时,系统自动做了向上造型
        master.drink(fish);
        master.drink(chick);

*/

        /*
        //演示 向上造型(多态)的第一点应用:
        Animal[] aniamal = new Animal[5];
        aniamal[0] = new Dog("小黑",2,"白");
        aniamal[1] = new Dog("小白",2,"黑");
        aniamal[2] = new Fish("小黄",5,"黄");
        aniamal[3] = new Fish("小金",2,"金");
        aniamal[4] = new Chick("小花",1,"花");
        for(int i = 0; i<aniamal.length;i++){//遍历所有动物
            System.out.println(aniamal[i].name);//输出每个动物的名字
            System.out.println(aniamal[i].age);//输出每个动物年龄
            System.out.println(aniamal[i].color);//输出每个动物颜色
            aniamal[i].eat();
            aniamal[i].drink();
        }

        */


        }
    }

